﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using CloudClipboardSync.Utils;

namespace CloudClipboardSync.Converters
{
    public class VisibilityConverter : IValueConverter
    {
        public VisibilityConverter()
        {
            VisibleValue = null;
            NonVisibleValue = null;
            Hide = false;
        }

        public object VisibleValue { get; set; }
        public object NonVisibleValue { get; set; }
        public bool Hide { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility)
            {
                var visible = (Visibility) value == Visibility.Visible;
                return visible ? VisibleValue : NonVisibleValue;
            }
            else
            {
                const Visibility visValue = Visibility.Visible;
                var nonVisValue = Hide ? Visibility.Hidden : Visibility.Collapsed;

                if (GuiTools.IsDesignTime)
                    return true;

                if (Equals(value, VisibleValue) || Equals((value ?? "null").ToString(), VisibleValue))
                    return visValue;
                else if (Equals(value, NonVisibleValue) || Equals((value ?? "null").ToString(), NonVisibleValue))
                    return nonVisValue;
                else if (VisibleValue == null)
                    return visValue;
                else if (NonVisibleValue == null)
                    return nonVisValue;
                else
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType, parameter, culture);
        }

        #endregion
    }
}
